package com.nameless.controller.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.nameless.entity.Coupon;
import com.nameless.entity.CouponType;
import com.nameless.service.CouponService;
import com.nameless.service.CouponTypeService;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/03/01 15:34
 * @Description
 */
@Api(value = "/coupon/web", tags = {"-*- coding:utf-8 -*-"})
@RestController
@RequestMapping("/coupon/web")
@Slf4j
public class WebCouponController {
    @Autowired
    private CouponService couponService;

    @Autowired
    private CouponTypeService couponTypeService;

    /**
     * 通过店面id获取当前可以领取的优惠劵
     *
     * @param shopId 商店id
     * @return {@link Result}<{@link List}<{@link Coupon}>>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "shopId", value = "商店id", required = true)
    })
    @ApiOperation(value = "通过店面id获取当前可以领取的优惠劵", notes = "通过店面id获取当前可以领取的优惠劵", httpMethod = "GET")
    @GetMapping("findByShopId/{shopId}")
    public Result<List<Coupon>> findByShopId(@PathVariable Integer shopId) {
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getShopId,shopId);
//        queryWrapper.eq(Coupon::getUseType,0);
        // gt（大于 >） ge（大于等于 >=）  lt（小于 <）  le（小于等于 <=）
        queryWrapper.le(Coupon::getEnableTime,new Date());
        queryWrapper.ge(Coupon::getEndTime,new Date());
        List<Coupon> list = couponService.list(queryWrapper);
        List<Coupon> collect = list.stream().peek(item -> {
            item.setStartAt(item.getStartTime().getTime() / 1000L);
            item.setEndAt(item.getEndTime().getTime() / 1000L);
        }).collect(Collectors.toList());
        return Result.success(collect);
    }

    /**
     * 通过 车id 获取当前可以领取的优惠劵
     *
     * @param carId 汽车id
     * @return {@link Result}<{@link List}<{@link Coupon}>>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "shopId", value = "商店id", required = true)
    })
    @ApiOperation(value = "通过车id获取当前可以领取的优惠劵", notes = "通过店面id获取当前可以领取的优惠劵", httpMethod = "GET")
    @GetMapping("/findByCarId/{carId}")
    public List<Coupon> findByShopIdAndCarId(@PathVariable Integer carId) {
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getUseType,2);
//        queryWrapper.eq(Coupon::getUseType,0);
        // gt（大于 >） ge（大于等于 >=）  lt（小于 <）  le（小于等于 <=）
        queryWrapper.le(Coupon::getEnableTime,new Date());
        queryWrapper.ge(Coupon::getEndTime,new Date());
        ArrayList<Coupon> arrayList = new ArrayList<>();

        List<Coupon> CouponList = couponService.list(queryWrapper);

        List<Coupon> collect = CouponList.stream().peek(coupon -> {
            List<String> list = Arrays.asList(coupon.getCarId().split(","));

            boolean b = list.stream().anyMatch(id -> id.equals(carId.toString()));

            if (b) {
                arrayList.add(coupon);
            }
        }).collect(Collectors.toList());

        return arrayList;
    }

    /**
     * 通过店面id 与类型id获取当前可以领取的优惠劵
     *
     * @param typeId id类型
     * @return {@link Result}<{@link List}<{@link Coupon}>>
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "int", name = "shopId", value = "商店id", required = true)
    })
    @ApiOperation(value = "通过店面id获取当前可以领取的优惠劵", notes = "通过店面id获取当前可以领取的优惠劵", httpMethod = "GET")
    @GetMapping("findByTypeId/{typeId}")
    public List<Coupon> findByShopIdAndTypeId(@PathVariable Integer typeId) {
        LambdaQueryWrapper<Coupon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Coupon::getUseType,1);
        // gt（大于 >） ge（大于等于 >=）  lt（小于 <）  le（小于等于 <=）
        queryWrapper.le(Coupon::getEnableTime,new Date());
        queryWrapper.ge(Coupon::getEndTime,new Date());
        ArrayList<Coupon> arrayList = new ArrayList<>();
        List<Coupon> CouponList = couponService.list(queryWrapper);
        List<Coupon> collect = CouponList.stream().peek(coupon -> {
            List<String> list = Arrays.asList(coupon.getTypeId().split(","));
            boolean b = list.stream().anyMatch(id -> id.equals(typeId.toString()));
            if (b) { arrayList.add(coupon); }
        }).collect(Collectors.toList());
        return arrayList;
    }

    @GetMapping("/typeCouponType/{id}")
    CouponType typeCouponType(@PathVariable Integer id){
        CouponType couponType = couponTypeService.getById(id);
        return couponType;
    }
}
